import DataType from './data-type.js'
import app from '@system.app';
const Constants = {
    SUBSCRIBE: 1001, // 订阅消息通知
    UNSUBSCRIBE: 1002, // 取消订阅消息
    SEND_COMMAND: 1003, // 发送命令
    CONNECT: 1008, // 连接
    DISCONNECT: 1009, // 断开连接
    NOTIFY_DEVICE_ID: 1006, // 设置deviceId
    RE_NET_CONFIG: 1107, // 重新配网
};

const getParams = function (code) {
    return {
        messageCode: code,
        bundleName: 'com.changhong.kqzg01',
        abilityName: 'com.chinasoftinc.facommon.control.service.HilinkDeviceServiceAbility',
        abilityType: 0,
        data: code,
    };
};

const TAG = 'hilink-devices'

const HiLinkDevice = {
    /**
     * 订阅消息事件, 包含设备连接, 数据上报下发等所有事件
     * 进入页面时调用此接口
     * @param callback 消息回调
     */
    subscribeEvent(callback) {
        let params = getParams(Constants.SUBSCRIBE);
        console.log(`${TAG} subscribeEven.start: ${JSON.stringify(params)}`)
        FeatureAbility.subscribeAbilityEvent(params, callback)
            .then(value => {
                console.log(`${TAG} subscribeEven.then: ${JSON.stringify(value)}`)
            }).catch(err => {
            console.log(`${TAG} subscribeEven.catch: ${JSON.stringify(err)}`)
        })
    },

    /**
     * 取消订阅消息事件
     * 退出FA时调用此接口
     */
    unsubscribeEvent() {
        let params = getParams(Constants.UNSUBSCRIBE);
        FeatureAbility.unsubscribeAbilityEvent(params)
            .then(value => {
                console.log(`${TAG} unsubscribeEvent.then: ${JSON.stringify(value)}`)
            }).catch(err => {
            console.log(`${TAG} unsubscribeEvent.catch: ${JSON.stringify(err)}`)
        })
    },
    /**
     * 连接设备
     */
    connect(cb) {
        let params = getParams(Constants.CONNECT);
        FeatureAbility.callAbility(params)
            .then(value => {
                console.log(`${TAG} connect.then: ${JSON.stringify(value)}`);
                if(value){
                    cb(value);
                }
//                setTimeout(()=>{
//                    HiLinkDevice.disConnect();
//                    setTimeout(()=>{
//                        app.terminate();
//                    },100)
//                },3000)

            }).catch(err => {
            console.log(`${TAG} connect.catch: ${JSON.stringify(err)}`)
        })
    },
    myDisconnect(newTime){
        let tt=Date.now()-newTime;
        if((tt)<0){
            console.log("请先连接");
        }else if (tt<3000){
            setTimeout(()=>{
                HiLinkDevice.disConnect();
            },3000-tt)

        }else{
            HiLinkDevice.disConnect();
        }
    },
    /**
     * 断开连接
     */
    disConnect(cb) {
        let params = getParams(Constants.DISCONNECT);
        FeatureAbility.callAbility(params)
            .then(value => {
               typeof cb  === 'function' && cb();
                console.log(`${TAG} disConnect.then: ${JSON.stringify(value)}`)
            }).catch(err => {
            console.log(`${TAG} disConnect.catch: ${JSON.stringify(err)}`)
        })
    },
    /**
     * 发送命令
     * @param serviceId 指令ID
     * @param param 属性:属性值, 格式为key-value键值对
     * @eg device.sendCommand('lampswitch', {on: 1})
     */
    sendCommand(serviceId, param) {
        let params = getParams(Constants.SEND_COMMAND);
        params.data = {
            serviceId,
            param,
        };
        params.syncOption = 1;
        console.log(`${TAG} sendCommand.then:${serviceId}${param}`)
        FeatureAbility.callAbility(params)
            .then(value => {
                console.log(`${TAG} sendCommand.then: ${JSON.stringify(value)}`)
            }).catch(err => {
            console.log(`${TAG} sendCommand.catch: ${JSON.stringify(err)}`)
        })
    },
    /**
     * 刷新DevicesID
     * @param deviceId 设备ID
     */
    notifyDeviceId(deviceId) {
        let params = getParams(Constants.NOTIFY_DEVICE_ID);
        params.data = { "deviceId": deviceId };
        params.syncOption = 0;
        FeatureAbility.callAbility(params)
            .then(value => {
                console.log(`${TAG} notifyDeviceId.then: ${JSON.stringify(value)}`)
            }).catch(err => {
            console.log(`${TAG} notifyDeviceId.catch: ${JSON.stringify(err)}`)
        })
    },

    /**
     * 打开hilink配网, 用于重新配网
     */
    reNetConfig() {
        let params = getParams(Constants.RE_NET_CONFIG);
        FeatureAbility.callAbility(params)
            .then(value => {
                console.log(`${TAG} openHilinkRegister.then: ${JSON.stringify(value)}`)
            }).catch(err => {
            console.log(`${TAG} openHilinkRegister.catch: ${JSON.stringify(err)}`)
        })
    },
    /**
     * 格式化消息事件类型, 调试用
     * @param type int类型事件
     */
    getDataTypeDes(type) {
        let action = ''
        switch (type) {
            case DataType.GET_DEVICE:
                action = 'GET_DEVICE'
                break
            case DataType.UPDATE_DEVICEID:
                action = 'UPDATE_DEVICEID'
                break
            case DataType.BLE_CHARACTERISTIC_CHANGED:
                action = 'BLE_CHARACTERISTIC_CHANGED'
                break
            case DataType.BLE_CONNECTION_STATE_CHANGED:
                action = 'BLE_CONNECTION_STATE_CHANGED'
                break
            case DataType.WIFI_GET_PROFILE_DATA:
                action = 'WIFI_GET_PROFILE_DATA'
                break
            case DataType.DEVICE_ADD_LISTENER:
                action = 'DEVICE_ADD_LISTENER'
                break
            case DataType.DEVICE_ATTACH_LISTENER:
                action = 'DEVICE_ATTACH_LISTENER'
                break
            case DataType.DEVICE_DELETE_LISTENER:
                action = 'DEVICE_DELETE_LISTENER'
                break
            case DataType.DEVICE_INFO_LISTENER:
                action = 'DEVICE_INFO_LISTENER'
                break
            case DataType.PROFILE_DATA_LISTENER:
                action = 'PROFILE_DATA_LISTENER'
                break
            case DataType.SEND_COMMAND_CALLBACK:
                action = 'SEND_COMMAND_CALLBACK'
        }
        return `${action}: ${type}`
    },
};

export default HiLinkDevice;